package main

import (
	"github.com/robfig/cron/v3"
	"time"
)


func init() {

	initEtcd()

	resultChan = make(chan *Result, 1000)

	task1 := new(Task)
	task1.Id = 1
	task1.Spec = "*/5 * * * * *"
	task1.Command = "php -v"

	task2 := new(Task)
	task2.Id = 2
	task2.Spec = "*/10 * * * * *"
	task2.Command = "ls -l"

	tasks.Store("task1", task1)
	tasks.Store("task2", task2)

	secondParser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor)
	now := time.Now()

	tasks.Range(func(key, value interface{}) bool {
		task, _ := value.(*Task)
		schedule, _ := secondParser.Parse(task.Spec)
		task.Schedule = schedule
		task.NextTime = task.Schedule.Next(now)

		return true
	})
}